<!--引入init配置文件-->
<?php
    require "init.php";

    //没有登录,不能进入到付款页面
    if(empty($_SESSION['home'])){
        redirect("请登录",1,ROOT_URL."login.php");
        die;
    }
    //获取该用户id
    $user_id = $_SESSION['home']['id'];

    if(empty($_POST['adress'])){
        redirect("亲,您还没有设置收货地址");die;
    }
    //接受收货地址id
    $address_id = $_POST['adress'];
    //判断该用户是否有默认地址
    $sql = "select address from " . PRE ."user_address where user_id = '$user_id' and id = '$address_id'";
    $res = getRow($link, $sql);
    if(!$res){
        redirect("亲,您还没有收货地址");die;
    }
    $address = $res['address'];



    //得到商品种类数,因为在表单中循环传了商品id 和 商品数量 和 商品价格 所以除3
    $goods_num = floor(count($_POST)/3);
    $total_price = 0;
    for($i = 0; $i < $goods_num;$i++){
        //得到每一个商品的id
        $goods_id = $_POST[$i];
        $good_id_arr[$i] = $goods_id;
        //得到相应的商品的价格
        $goods_price = $_POST['price'.$goods_id];
        $goods_price_arr[$i] = $goods_price;
        //得到要购买的商品的数量
        $qty = $_POST['qty'.$goods_id];
        $qty_arr[$i] = $qty;
        //用sql语句查出每件商品的库存,看是否满足该用户的购买量
        $sql = "select gname,stock from ".PRE ."goods where id='$goods_id'limit 1";
        $good_info = getRow($link, $sql);
        $goods_gname_arr[$i] = $good_info['gname'];
        $stock = $good_info['stock'];
        if($qty > $stock){
            redirect("很抱歉,<span class='text-danger'>".$good_info['gname']."</span>的数量不足$qty",1);
            die;
        }
        //求出每一种商品的总价,在累加
        $total_price +=  $goods_price * $qty;
    }
    //插入订单表
    //获得该用户的电话
    $sql = "select tel,name from ".PRE."user where id = '$user_id'";
    $res = getRow($link, $sql);
    $phone = $res['tel'];
    //生成唯一订单编号
    $ordernum = date("dHis").mt_rand(0,100);
    //插入订单表
    //获取,用户名,地址,总价格
    $oname = $res['name'];
    //编写sql
    $sql = "insert into ".PRE."order
                (ordernum,user_id,oname,phone,address,allprice,create_time) values
                ('$ordernum','$user_id','$oname','$phone','$address','$total_price','".DATE."')";
    //执行sql
    $order_id = execute($link,$sql);
    if(!$order_id){
        redirect('订单插入失败');die;
    }
    //插入订单商品表
    for($i = 0; $i < $goods_num;$i++){
        //得到每一个商品的id
        $goods_id = $good_id_arr[$i];
        //得到相应的商品的价格
        $goods_price = $goods_price_arr[$i];
        //得到要购买的商品的数量
        $qty = $qty_arr[$i];
        //把订单中的每一件商品分别插入到订单商品表
        $sql = "insert into ".PRE."ordergoods
                (goods_id,price,qty,order_id,create_time) values
                ('$goods_id','$goods_price','$qty','$order_id','".DATE."')";
        //返回刚插入的订单的id
        $ordergoods_id = execute($link,$sql);
        //把订单id放到数组中
        $ordergoods_id_arr[$i] = $ordergoods_id;
        if(!$ordergoods_id){
            //如果有任何一个订单商品插入失败,就把刚插入到订单表中的记录删除,
            //再把刚才插入成功的订单商品给删掉
            $sql ="delete from ".PRE."order where id = $order_id";
            execute($link,$sql);
            for($i = 0; $i < $goods_num;$i++){
                $ordergood_id = $ordergoods_id_arr[$i];
                $sql ="delete from ".PRE."ordergoods where id = '$ordergood_id'";
                execute($link,$sql);
            }
            redirect("订单商品写入失败");

        }

    }
    //最后再把购物车中的相关商品删除
    for($i = 0; $i < $goods_num;$i++){
        //得到每一个商品的id
        $goods_id = $good_id_arr[$i];
        $sql = "delete from ".PRE ."cart where goods_id = '$goods_id' and user_id = '$user_id'";
        execute($link,$sql);
    }



?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <link rel="icon" href="./public/home/imgs/logo.jpg">
    <title>小米手机专卖店(ximiao)</title>

    <!-- Bootstrap -->
    <link href="./public/home/css/bootstrap.min.css" rel="stylesheet">
    <link href="./public/home/css/index.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="./public/home/js/html5shiv.min.js"></script>
    <script src="./public/home/js/respond.min.js"></script>
    <![endif]-->
</head>
<body>
    <!--引入导航条-->
    <?php require ROOT_PATH."./com/nav.php" ?>

    <div class="container mt50">
        <div class="row mt50">
            <h3>收货地址: <b><?php echo $address;?></b></h3>
            <h3>付款金额: <b><?php echo $total_price;?></b></h3>
            <form action="<?php echo ROOT_URL?>com/buydo.php?act=pay" method="post">
                <input type="hidden" name="order_id" value="<?php echo $order_id;?>">
                <input type="submit" value="付款" class="btn btn-danger">
            </form>

        </div>
    </div>


    <!--引入底部开始-->
    <?php require ROOT_PATH."./com/footer.php" ?>


<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="./public/home/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="./public/home/js/bootstrap.min.js"></script>
</body>
</html>

